
package guoan.service;

import guoan.core.common.WebConfig;
import guoan.core.dto.RestResponse;
import guoan.domain.ExamConfig;
import guoan.repository.ExamConfigRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false)
public class ExamConfigService {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ExamConfigRepository examConfigRepository;
    @Autowired
    private KeywordService keywordService;

    public ExamConfig findExamConfig(){
        // TODO Auto-generated method stub
        return examConfigRepository.findOne(1L);
    }

    public RestResponse<Boolean> update(ExamConfig examConfig){
        // TODO Auto-generated method stub
        ExamConfig cco = findExamConfig();
        cco.setContent(examConfig.getContent());
        String keyName = examConfig.getKeyName();

        if (!keyName.equals(cco.getKeyName())) {
            keywordService
                    .delete(WebConfig.KEYWORD_KEY_INFO_EXAM, cco.getId());
            keywordService.add(keyName, WebConfig.KEYWORD_KEY_INFO_EXAM,
                    cco.getId());
            cco.setKeyName(keyName);
        }
        examConfigRepository.save(cco);

        return RestResponse.ok(true);
    }
}
